package com.offcn.controller;

import com.offcn.dao.StudentRepository;
import com.offcn.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/mongo")
public class MongoDBController {
    @Autowired
    StudentRepository studentRepository;

    @GetMapping("/findAll")
    public List<Student> findAll(){
        /*findAll方法就是StudentRepository默认实现的*/
        List<Student> all = studentRepository.findAll();
        return all;
    }

    @GetMapping("/findByScode/{scode}")
    public Student findByScode(@PathVariable String scode){
        Student student = studentRepository.findByScode(scode);
        return student;
    }

    @PostMapping("/save")
    public void save(Student student){
        studentRepository.insert(student);
    }

    @DeleteMapping("/delByScode/{scode}")
    public void delByScode(@PathVariable String scode){
        studentRepository.deleteByScode(scode);
    }

}
